#import "RecruitDetailModule.h"

@interface RecruitDetailModule ()

@end

@implementation RecruitDetailModule
@synthesize jobBtn;
@synthesize companyBtn;
@synthesize jobTitleLb;
@synthesize addTimeLb;
@synthesize companyDic;
@synthesize salaryLb;
@synthesize salaryValLb;
@synthesize merchantNameLb;
@synthesize merchantNameValLb;
@synthesize addressLb;
@synthesize addressValLb;
@synthesize zhaopinTjLb;
@synthesize contenttextview;
@synthesize henxianView1;
@synthesize henxianView2;
@synthesize henxianView3;

@synthesize jobDescLb;
@synthesize jobDescTv;
@synthesize linkInfoLb;
@synthesize linkmanLb;
@synthesize linkmanValLb;
@synthesize mpLb;
@synthesize emailLb;
@synthesize emailValLb;
@synthesize companyAddressLb;
@synthesize companyAddrValLb;
@synthesize lastTipLb;
@synthesize mpValLb;

@synthesize rectuitId;

#pragma mark - MWPhotoBrowserDelegate

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return _photos.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < _photos.count)
        return [_photos objectAtIndex:index];
    return nil;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index {
    if (index < _thumbs.count)
        return [_thumbs objectAtIndex:index];
    return nil;
}
-(void)yourHandlingCode:(UIGestureRecognizer *)gestureRecognizer
{
    NSLog(@"点击图片");
    
    UIImageView *view = (UIImageView*)[gestureRecognizer view];
    int tag = view.tag;
    
    
    BOOL displayActionButton = YES;
    BOOL displaySelectionButtons = NO;
    BOOL displayNavArrows = NO;
    BOOL enableGrid = YES;
    BOOL startOnGrid = NO;
    
    
    //Create browser
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    
    browser.displayActionButton = displayActionButton;
    browser.displayNavArrows = displayNavArrows;
    browser.displaySelectionButtons = displaySelectionButtons;
    browser.alwaysShowControls = displaySelectionButtons;
    browser.zoomPhotosToFill = YES;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
    browser.wantsFullScreenLayout = YES;
#endif
    browser.enableGrid = enableGrid;
    browser.startOnGrid = startOnGrid;
    browser.enableSwipeToDismiss = YES;
    
    
    [browser setCurrentPhotoIndex:tag];
    
    
    [self.navigationController pushViewController:browser animated:YES];
    
    
    double delayInSeconds = 3;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        
        
    });
    
    
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.separatorStyle=NO;
    
    self.photos=[[NSMutableArray alloc] initWithCapacity:8];
    self.thumbs = [[NSMutableArray alloc] init];
    
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
    
    
    
    
    self.view.backgroundColor=[UIColor whiteColor];
    UILabel* titleLb=[[UILabel alloc] initWithFrame:CGRectMake(90, 0, 140, 40)];
    titleLb.text=@"招聘详情";
    titleLb.font=[UIFont boldSystemFontOfSize:HEAD_SIZE];
    titleLb.textColor=[UIColor whiteColor];
    titleLb.textAlignment=NSTextAlignmentCenter;
    titleLb.backgroundColor=[UIColor clearColor];
    self.navigationItem.titleView=titleLb;
    
    UIButton* cancelBtn= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    [cancelBtn addTarget:self action:@selector(backFn) forControlEvents:UIControlEventTouchUpInside];
    CGRect frame_1= CGRectMake(0,12, 20,20);
    UIImageView*  backView=[[UIImageView alloc] initWithFrame:frame_1];
    UIImage* image= [UIImage imageNamed:@"back2016"];
    backView.image=image;
    [cancelBtn addSubview:backView];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:cancelBtn];
    
    
    
    UIButton*  addFocusBtn=[[UIButton alloc] initWithFrame:CGRectMake(240, 5, 30, 34)];
    [addFocusBtn addTarget:self action:@selector(addFocus) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel* jubaoLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 34)];
    jubaoLb.text=@"+关注";
    jubaoLb.font=[UIFont systemFontOfSize:14];
    jubaoLb.textColor=[UIColor whiteColor];
    jubaoLb.backgroundColor=[UIColor clearColor];
    [addFocusBtn addSubview:jubaoLb];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:addFocusBtn];
    
    
    
    
    [NSThread detachNewThreadSelector:@selector(loadRecruitDetailFn) toTarget:self withObject:nil];
    
    
    
    
    
    
}
-(void)backFn{
    [self.navigationController popViewControllerAnimated:NO];
}
-(void)loadJobThreadFn{
    
    [self.merchantNameLb removeFromSuperview];
    [self.addressLb removeFromSuperview];
    [self.addressValLb removeFromSuperview];
    [self.zhaopinTjLb removeFromSuperview];
    [self.contenttextview removeFromSuperview];
    
    [self.logoView removeFromSuperview];
    [self.linkInfoLb  removeFromSuperview];
    [self.linkmanLb  removeFromSuperview];
    [self.linkmanValLb  removeFromSuperview];
    [self.mpLb  removeFromSuperview];
    [self.emailLb  removeFromSuperview];
    [self.emailValLb  removeFromSuperview];
    [self.companyAddressLb  removeFromSuperview];
    [self.companyAddrValLb  removeFromSuperview];
    [self.lastTipLb  removeFromSuperview];
    [self.mpValLb  removeFromSuperview];
    
    [self.henxianView1 removeFromSuperview];
    [self.henxianView2 removeFromSuperview];
    [self.henxianView3 removeFromSuperview];
    
    
    
    [NSThread detachNewThreadSelector:@selector(loadRecruitDetailFn) toTarget:self withObject:nil];
}
-(void)loadRecruitDetailFn{
    NSString *url=@"";
    
    url=[HOST stringByAppendingString:@"index.php/appRecruit/loadRecruit?recruit_id="];
    NSMutableString *mUrl=[[NSMutableString alloc] initWithString:url];
    NSString  *sRectuitId=[NSString stringWithFormat:@"%d",self.rectuitId];
    [mUrl appendString:sRectuitId];
    
    
    NSLog(@"murl===%@",mUrl);
    
    
    NSError *error;
    NSString *jsonStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:mUrl] encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"jsonStr===%@",jsonStr);
    
    self.dic=[NSDictionary dictionaryWithDictionary:[jsonStr JSONValue]];
    
    
    [self performSelectorOnMainThread:@selector(didFinishRequest) withObject:nil waitUntilDone:YES];
    
    
    
}

-(float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{
    float fPadding = 16.0; // 8.0px x 2
    CGSize constraint = CGSizeMake(textView.contentSize.width - fPadding, CGFLOAT_MAX);
    
    CGSize size = [strText sizeWithFont: textView.font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    
    float fHeight = size.height + 16.0;
    
    return fHeight;
}

-(void)didFinishRequest{
    
    NSString* strMerchant_id=[self.dic objectForKey:@"merchant_id"];
    merchant_id=[strMerchant_id intValue];
    int y=-20;
    //
    NSString*  logo=[self.dic objectForKey:@"logo"];
    logo=[NSString stringWithFormat:@"%@",logo];
    if(logo!=nil&&logo.length>6){
        logo=[HOST stringByAppendingString:logo];
        NSURL*  url=[NSURL URLWithString:logo];
        UIButton*  merchantBtn=[[UIButton alloc] initWithFrame:CGRectMake(10, 10, 60, 60)];
        [merchantBtn setImageWithURL:url forState:0];
        [self.view addSubview:merchantBtn];
    }
    
    NSString* merchant_name=[self.dic objectForKey:@"merchant_name"];
    merchant_name=[NSString stringWithFormat:@"%@",merchant_name];
    UILabel*  merchant_nameLb=[[UILabel alloc] initWithFrame:CGRectMake(85, 12, 200, 20)];
    merchant_nameLb.text=merchant_name;
    merchant_nameLb.font=[UIFont systemFontOfSize:14];
    [self.view addSubview:merchant_nameLb];
    //
    NSString* qm=[self.dic objectForKey:@"qm"];
    qm=[NSString stringWithFormat:@"签名:%@",qm];
    UILabel*  qmLb=[[UILabel alloc] initWithFrame:CGRectMake(85, 50, 200, 20)];
    qmLb.text=qm;
    qmLb.textColor=[UIColor grayColor];
    qmLb.font=[UIFont systemFontOfSize:14];
    [self.view addSubview:qmLb];
    
    
    
    self.jobTitleLb=[[UILabel alloc] initWithFrame:CGRectMake(12, 80, 150, 20)];
    self.jobTitleLb.textAlignment=UITextAlignmentLeft;
    NSString* title=[self.dic objectForKey:@"title"];
    self.jobTitleLb.text=title;
    self.jobTitleLb.font=[UIFont systemFontOfSize:14];
    self.jobTitleLb.textColor=[UIColor blackColor];
    [self.view addSubview:self.jobTitleLb];
    //
    UIImageView*  auth=[[UIImageView alloc] initWithFrame:CGRectMake(255,22, 115/2, 44/2)];
    auth.image=[UIImage imageNamed:@"auth"];
    [self.view addSubview:auth];
    
    int  yy=50;
    self.addTimeLb=[[UILabel alloc] initWithFrame:CGRectMake(10, 100, 150, 20)];
    self.addTimeLb.textAlignment=UITextAlignmentLeft;
    NSString* add_time=[self.dic objectForKey:@"add_time"];
    add_time=[add_time substringToIndex:10];
    self.addTimeLb.text=add_time;
    self.addTimeLb.textColor=[UIColor grayColor];
    self.addTimeLb.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:self.addTimeLb];
    
    UIImage* henxian=[UIImage imageNamed:@"横线.png"];
    self.henxianView1=[[UIImageView alloc] initWithFrame:CGRectMake(10, 140, 300, 1)];
    self.henxianView1.image=henxian;
    [self.view addSubview:self.henxianView1];
    
    
    
    //
    self.merchantNameLb=[[UILabel alloc] initWithFrame:CGRectMake(10, y+83+30, 60, 20)];
    self.merchantNameLb.textAlignment=UITextAlignmentLeft;
    
    
    
    UIImage* aoxian=[UIImage imageNamed:@"aoxian.png"];
    UIImageView*  aoxianView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 140, 320, 2.5)];
    aoxianView.image=aoxian;
    [self.view addSubview:aoxianView];
    
    
    
    
    self.zhaopinTjLb=[[UILabel alloc] initWithFrame:CGRectMake(10, y+88+yy+30, 60, 20)];
    self.zhaopinTjLb.textAlignment=UITextAlignmentLeft;
    
    
    self.zhaopinTjLb.text=@"招聘详情:";
    self.zhaopinTjLb.textColor=[UIColor blackColor];
    self.zhaopinTjLb.font=[UIFont boldSystemFontOfSize:13];
    [self.view addSubview:self.zhaopinTjLb];
    
    y=y+30;
    self.contenttextview=[[UITextView alloc] initWithFrame:CGRectMake(10, y+88+yy+50, 280, 140)];
    self.contenttextview.text=[self.dic objectForKey:@"tj"];
    
    [self.view addSubview:self.contenttextview];
    NSInteger newSizeH;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    {
        
        float fPadding = 16.0; // 8.0px x 2
        
        CGSize constraint = CGSizeMake(self.contenttextview.contentSize.width - fPadding, CGFLOAT_MAX);
        
        CGSize size = [self.contenttextview.text sizeWithFont: self.contenttextview.font
                                            constrainedToSize:constraint
                                                lineBreakMode:UILineBreakModeWordWrap];
        newSizeH = size.height + 16.0 - 6;
        self.contenttextview.frame=CGRectMake(20 ,y+88+yy+30,280,newSizeH);
        self.contenttextview.contentSize=CGSizeMake(280, newSizeH);
        
        newSizeH=self.contenttextview.contentSize.height;
    }
    else
    {
        CGSize size = [[self.contenttextview text] sizeWithFont:[self.contenttextview font]];
        int length = size.height;  // 2. 取出文字的高度
        int colomNumber = self.contenttextview.contentSize.height/length;  //3. 计算行数
        self.contenttextview.frame=CGRectMake(20 ,y+88+yy+30,280,colomNumber*22);
        self.contenttextview.contentSize=CGSizeMake(280, colomNumber*22);
        
        newSizeH=colomNumber*22;
        
    }
    //下面设置禁止滚动
    if (self.contenttextview.contentSize.height <= self.contenttextview.frame.size.height) {
        
        [self.contenttextview setUserInteractionEnabled:NO];
    }
    
    //
    int y1=y+88;
    
    NSString*  pic0=[self.dic objectForKey:@"pic0"];
    pic0=[NSString stringWithFormat:@"%@",pic0];
    
    NSString*  pic1=[self.dic objectForKey:@"pic1"];
    pic1=[NSString stringWithFormat:@"%@",pic1];
    
    NSString*  pic2=[self.dic objectForKey:@"pic2"];
    pic2=[NSString stringWithFormat:@"%@",pic2];
    
    
    
    
    int  h1=[self heightForTextView: self.contenttextview WithText: [self.dic objectForKey:@"tj"]];
    
    
    self.henxianView2=[[UIImageView alloc] initWithFrame:CGRectMake(10, y1+h1+10+30, 300, 1)];
    self.henxianView2.image=henxian;
    [self.view addSubview:self.henxianView2];
    
    
    
    
    int n=4;
    UIScrollView* sv=[[UIScrollView alloc] initWithFrame:CGRectMake(0, y1+h1+20+yy+30, 320, 180)];
    
    //sv.accessibilityActivationPoint = CGPointMake(110*n, 100);
    //sv.contentSize = CGSizeMake(400,0);
    sv.showsHorizontalScrollIndicator = FALSE;
    sv.delegate = self;
    
    [self.view addSubview:sv];
    
    
    
    UIImageView* view=nil;
    NSString* pic3=[self.dic objectForKey:@"pic3"];
    pic3=[NSString stringWithFormat:@"%@",pic3];
    NSLog(@"图片地址==pic0=====%@",pic0);
    
    MWPhoto* mwPhoto=nil;
    
    
    if (pic0!=nil&&pic0.length>0) {
        pic0=[@"upload/" stringByAppendingString:pic0];
        pic0=[HOST stringByAppendingString:pic0];
        NSLog(@"图片地址==pic0=====%@",pic0);
        view=[[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 300, 140)];
        [view setImageWithURL:[NSURL URLWithString:pic0]];
        view.tag=0;
        
        view.userInteractionEnabled=YES;//这句话必须加 否则无法点击
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
        [view addGestureRecognizer:singleTap];
        
        mwPhoto=[MWPhoto photoWithURL:[NSURL URLWithString:pic0]];
        
        [self.photos addObject:mwPhoto];
        [self.thumbs addObject:mwPhoto];
        
        [sv addSubview:view];
        
    }
    if (pic1!=nil&&pic1.length>0) {
        pic1=[@"upload/" stringByAppendingString:pic1];
        pic1=[HOST stringByAppendingString:pic1];
        NSLog(@"图片地址==pic1=====%@",pic1);
        view=[[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 300, 180)];
        [view setImageWithURL:[NSURL URLWithString:pic1]];
        view.tag=1;
        
        view.userInteractionEnabled=YES;//这句话必须加 否则无法点击
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
        [view addGestureRecognizer:singleTap];
        
        mwPhoto=[MWPhoto photoWithURL:[NSURL URLWithString:pic1]];
        
        [self.photos addObject:mwPhoto];
        [self.thumbs addObject:mwPhoto];
        
        [sv addSubview:view];
        
    }
    if (pic2!=nil&&pic2.length>0) {
        pic2=[@"upload/" stringByAppendingString:pic2];
        pic2=[HOST stringByAppendingString:pic2];
        NSLog(@"图片地址==pic1=====%@",pic2);
        view=[[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 300, 180)];
        [view setImageWithURL:[NSURL URLWithString:pic2]];
        view.tag=2;
        
        view.userInteractionEnabled=YES;//这句话必须加 否则无法点击
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
        [view addGestureRecognizer:singleTap];
        
        mwPhoto=[MWPhoto photoWithURL:[NSURL URLWithString:pic2]];
        
        [self.photos addObject:mwPhoto];
        [self.thumbs addObject:mwPhoto];
        
        [sv addSubview:view];
        
    }
    if (pic3!=nil&&pic3.length>0) {
        pic3=[@"upload/" stringByAppendingString:pic3];
        pic3=[HOST stringByAppendingString:pic3];
        NSLog(@"图片地址==pic3=====%@",pic3);
        view=[[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 300, 180)];
        [view setImageWithURL:[NSURL URLWithString:pic3]];
        view.tag=3;
        
        view.userInteractionEnabled=YES;//这句话必须加 否则无法点击
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
        [view addGestureRecognizer:singleTap];
        
        mwPhoto=[MWPhoto photoWithURL:[NSURL URLWithString:pic3]];
        
        [self.photos addObject:mwPhoto];
        [self.thumbs addObject:mwPhoto];
        
        [sv addSubview:view];
        
    }
    
    
    
    
    int jobY0=y1+h1+10+130;
    self.jobDescTv=[[UITextView alloc] initWithFrame:CGRectMake(10, jobY0, 280, 140)];
    self.jobDescTv.text=[self.dic objectForKey:@"job_desc"];
    
    [self.view addSubview:self.jobDescTv];
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    {
        NSInteger newSizeH;
        float fPadding = 16.0; // 8.0px x 2
        
        CGSize constraint = CGSizeMake(self.jobDescTv.contentSize.width - fPadding, CGFLOAT_MAX);
        
        CGSize size = [self.jobDescTv.text sizeWithFont: self.jobDescTv.font
                                      constrainedToSize:constraint
                                          lineBreakMode:UILineBreakModeWordWrap];
        newSizeH = size.height + 16.0 - 6;
        self.jobDescTv.frame=CGRectMake(20 ,jobY0,280,newSizeH);
        self.jobDescTv.contentSize=CGSizeMake(280, newSizeH);
    }
    else
    {
        CGSize size = [[self.jobDescTv text] sizeWithFont:[self.jobDescTv font]];
        int length = size.height;  // 2. 取出文字的高度
        int colomNumber = self.jobDescTv.contentSize.height/length;  //3. 计算行数
        self.jobDescTv.frame=CGRectMake(20 ,jobY0,280,colomNumber*22);
        self.jobDescTv.contentSize=CGSizeMake(280, colomNumber*22);
    }
    //下面设置禁止滚动
    if (self.jobDescTv.contentSize.height <= self.jobDescTv.frame.size.height) {
        
        [self.jobDescTv setUserInteractionEnabled:NO];
    }
    self.jobDescTv.backgroundColor=[UIColor clearColor];
    
    
    //self.tableView.contentInset=UIEdgeInsetsMake(0, 0, jobY0+h1+15+30+5+30*4+40+70, 0);
    
    
    
    
    
}

-(void)addFocus{
    NSString* loginURL=HOST;
    loginURL=[loginURL stringByAppendingString:@"/index.php/AppNearby/gzMerchantFn"];
    NSURL* url=[[NSURL alloc] initWithString:loginURL];
    ASIFormDataRequest *req=[ASIFormDataRequest requestWithURL:url];
    NSUserDefaults*  ud=[[NSUserDefaults alloc] init];
    NSString*  uid=[ud objectForKey:@"uid"];
    NSString  *sRectuitId=[NSString stringWithFormat:@"%d",self.merchant_id];
    
    [req addPostValue:uid forKey:@"uid"];
    [req addPostValue:sRectuitId forKey:@"target_uid"];
    [req startSynchronous];
    NSError *error = [req error];
    if (!error) {
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [MBProgressHUD hideHUDForView:app.window animated:YES];
        NSString *response = [req responseString];
        NSLog(@"返回结果..%@",response);
        
        self.dic=[NSMutableDictionary dictionaryWithDictionary:[response JSONValue]];
        NSDictionary *ddic = [response JSONValue];
        NSString* _code=[self.dic objectForKey:@"code"];
        int code=[[ddic objectForKey:@"code"]intValue];
        if(code==1){
            [self.view makeToast:@"关注成功" duration:1.0 position:@"center"];
        }
        if(code==7){
           [self.view makeToast:@"不能重复关注" duration:1.0 position:@"center"];
        }

    }
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}


@end
